package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Permission;
import com.itheima.service.RoleService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 角色管理
 */
@Transactional
@RestController
@RequestMapping("/role")
public class RoleController {
    @Reference
    RoleService service;

    /**
     * 分页
     *
     * @param bean
     * @return
     */
    @RequestMapping("/findByPage.do")
    public PageResult findPage(@RequestBody QueryPageBean bean) {
        PageResult result = service.findPage(bean);
        return result;
    }



    /**
     * 根据id查询没有的权限
     * @param id
     * @return
     */
    @RequestMapping("/findById.do")
    public Result findById(Integer id) {
        List<Permission> list = service.findById(id);
        if (list!=null&&list.size()>0){
            return new Result(true,"",list);
        }
        return new Result(false,"已拥有所有权限！");
    }



    /**
     * 添加权限
     *
     * @param id
     * @param permissionIds
     * @return
     */
    @RequestMapping("/addPermission.do")
    public Result add(Integer id, Integer[] permissionIds) {
        try {
            service.addaddPermission(id, permissionIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_PERMISSION_FAIL);
        }
        return new Result(true, MessageConstant.ADD_PERMISSION_SUCCESS);
    }

}
